package com.cookie; /**
 * @author wzm
 * @create 2023-05-10 14:47
 */

import com.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/cookie03")
public class CookieServlet03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //修改Cookie
        //方案一 创建一个同名的Cookie重新保存
       /* Cookie cookie = new Cookie("username","ww");
        response.addCookie(cookie);*/

        //方式二 获取所有的Cookie然后找到cookie使用setValue赋新值 然后通知客户端保存
        Cookie cookie = CookieUtils.findCookie(request.getCookies(), "username");
        if(cookie != null){
            //修改Cookie的值
            cookie.setValue("ls");
            response.addCookie(cookie);
        }else{
            response.getWriter().write("cookie不存在");
        }
    }
}
